// pages/contract/submit.js
const app = getApp()
const network = require("../../utils/network");
import util from "../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    readonly: true,
    create: true,
    can_edit: false,
    isReplenish: false,
    wf_data_id: null,
    contract_id: null,

    can_check: false,
    flow_status: null,
    flow_applyer_id: null,

    origin_id: -1,
    historyContractShow: false,

    user_id: wx.getStorageSync('user_id'),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options) {
      if (!!options.id && !!options.wf_data_id) {
        this.setData({
          contract_id: options.id,
          wf_data_id: options.wf_data_id,
          create: false
        })
        wx.setNavigationBarTitle({
          title: '合同信息'
        });
      }
      if (!!options.origin_id) {
        this.setData({
          origin_id: options.origin_id,
          isReplenish: true
        })
        wx.setNavigationBarTitle({
          title: '补充协议'
        });
      }
    }
  },

  onShow() {
    const that = this;
    that.selectComponent('.flow-btn-group').callbackBeforeConfirm = () => {
      return that.valid(that);
    };
  },

  getDetail({
    detail
  }) {
    console.log(detail)
    const that = this
    that.setData({
      can_check: !!detail.can_check,
      flow_status: !!detail.flow_status ? detail.flow_status : null,
      flow_applyer_id: !!detail.flow_applyer_id ? detail.flow_applyer_id : null,
      can_edit: (that.data.user_id==detail.flow_applyer_id) && (detail.flow_status==0||detail.flow_status==2||detail.flow_status==5)
    })
    if(detail.parent_id != null && detail.parent_id != undefined && detail.parent_id > 0){
      that.setData({
        origin_id: detail.parent_id,
        isReplenish: true
      })
      wx.setNavigationBarTitle({
        title: '补充协议'
      });
    }
  },

  showHistoryContractPopup() {
    this.setData({
      historyContractShow: true
    })
  },
  onClose() {
    this.setData({
      historyContractShow: false
    })
  },

  valid(that) {
    const detailCom = that.selectComponent('.contract-com');
    return detailCom.validForm(detailCom.data.contractInfo);
  },
  submit() {
    const that = this
    that.selectComponent('.contract-com').submit().then(() => {
      setTimeout(() => {
        that.back()
      }, 800)
    });
  },
  save() {
    const that = this
    that.selectComponent('.contract-com').save().then(() => {
      setTimeout(() => {
        that.back()
      }, 800)
    });
  },
  back() {
    wx.navigateBack({
      delta: 1
    })
  },
  pass({
    detail
  }) {
    const that = this
    return new Promise((resolve, reject) => {
      util.showLoading('通过中');
      network.PUT({
        uri: app.api.contractPass,
        params: {
          id: that.data.contract_id,
          wf_check_opinion: detail
        },
        success() {
          util.hideLoading();
          util.showToast('通过成功');
          setTimeout(() => {
            that.back()
          }, 800)
          resolve();
        },
        fail() {
          util.hideLoading();
          util.showToast('通过失败');
          reject();
        }
      })
    })
  },
  returnBack({
    detail
  }) {
    const that = this
    return new Promise((resolve, reject) => {
      util.showLoading('驳回中');
      network.PUT({
        uri: app.api.contractReturnBack,
        params: {
          id: that.data.contract_id,
          wf_check_opinion: detail
        },
        success() {
          util.hideLoading();
          util.showToast('驳回成功');
          setTimeout(() => {
            that.back()
          }, 800)
          resolve();
        },
        fail() {
          util.hideLoading();
          util.showToast('驳回失败');
          reject();
        }
      })
    })
  },
  del() {
    const that = this
    return new Promise((resolve, reject) => {
      util.showLoading('删除中');
      network.GET({
        uri: app.api.contractDelete,
        params: {
          id: that.data.contract_id
        },
        success() {
          util.hideLoading();
          util.showToast('删除成功');
          setTimeout(() => {
            that.back()
          }, 800)
          resolve();
        },
        fail() {
          util.hideLoading();
          util.showToast('删除失败');
          reject();
        }
      })
    })
  },
  actDel() {
    const that = this
    return new Promise((resolve, reject) => {
      util.showLoading('彻底删除中');
      network.GET({
        uri: app.api.contractActualDelete,
        params: {
          id: that.data.contract_id
        },
        success() {
          util.hideLoading();
          util.showToast('彻底删除成功');
          setTimeout(() => {
            that.back()
          }, 800)
          resolve();
        },
        fail() {
          util.hideLoading();
          util.showToast('彻底删除失败');
          reject();
        }
      })
    })
  },
})